<template>
  <div class="options">
    <svg-icon icon-class="more" class="icon" @click.stop="showDetail" />
    <!-- 展示详情 -->
    <div class="show-user" v-if="showUser" v-click-outside="outsideClick">
      <img class="avatar" :src="checkUser.avatar ? checkUser.avatar : defaultAvatar" />
      <div class="line">
        <div class="key">姓名:</div>
        <div class="val name">{{ checkUser.name }}</div>
      </div>
      <div class="line" v-if="checkUser.depts.length > 0">
        <div class="key">部门:</div>
        <div class="val">
          <el-tag v-for="item in checkUser.depts" :key="item.id" size="mini">{{
            item.name
          }}</el-tag>
        </div>
      </div>
      <div class="line" v-if="checkUser.posts.length > 0">
        <div class="key">岗位:</div>
        <div class="val">
          <span v-for="item in checkUser.posts" :key="item.id" class="post">{{
            item.name
          }}</span>
        </div>
      </div>
      <div class="line" v-if="checkUser.roles.length > 0">
        <div class="key">角色:</div>
        <div class="val">
          <span v-for="item in checkUser.roles" :key="item.id" class="role">{{
            item.name
          }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "optionBtn",
  props: {
    user: {
      type: Object,
      default: {},
    },
    list: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      defaultAvatar: require('@/assets/images/profile.jpg'),
      showUser: false,
      checkUser: {
        avatar: "",
        depts: [],
        email: "",
        loginTime: "",
        name: "",
        nickName: "",
        posts: [],
        roles: [],
        tel: "",
        userName: "",
      },
    };
  },
  watch: {
    user: {
      immediate: true,
      handler(val) {
        this.showUser = false;
      },
    },
  },
  methods: {
    showDetail() {
      let arr = this.list.filter((e) => {
        return e.uid == this.user.receiver;
      });
      if (arr.length === 0) {
        return;
      }
      this.checkUser = arr[0];
      this.showUser = true;
    },
    outsideClick() {
      this.showUser = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.options {
  flex: 1;
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  position: relative;

  .icon {
    font-size: 34px;
    cursor: pointer;
    padding: 6px;
    color: #1ea5f4;

    &:hover {
      background-color: #f2f2f2;
      border-radius: 4px;
    }
  }

  //  展示详情
  .show-user {
    width: 200px;
    max-height: 300px;
    background-color: #fff;
    position: absolute;
    top: 0px;
    right: 0px;
    border-radius: 4px;
    z-index: 999;
    box-sizing: border-box;
    padding: 10px 20px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    box-shadow: 0px 2px 2px 2px rgba(125, 143, 155, 0.1);

    .avatar {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      margin-bottom: 10px;
    }

    .line {
      width: 100%;
      height: auto;
      box-sizing: border-box;
      padding: 4px 0px;
      font-size: 14px;
      display: flex;
      align-items: flex-start;
      justify-content: space-between;

      .key {
        color: #666;
      }

      .val {
        max-width: 70%;

        & span {
          float: right;
          margin: 0px 0px 4px 4px;
        }

        .post {
          color: #666;
        }

        .role {
          color: #333;
        }
      }

      .name {
        font-weight: bold;
        font-size: 16px;
      }
    }
  }
}
</style>
